﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LLClient.ClientServiceRef;
using LLClient.ServerProxy;

namespace LLClient.Portal
{
    /// <summary>
    /// Interaction logic for ChatControl.xaml
    /// </summary>
    public partial class ChatControl : UserControl
    {
        private Proxy _proxy = Proxy.GetInstance();
        private List<string> usernameOnline;
        private bool _eventRegistered = false;
        private Client _currentClient = Proxy.GetInstance().CurrentClient;
        private Client _dummyClient;
        public Client CurrentClient
        {
            get { return _currentClient; }
            set { _currentClient = value; }
        }

        public ChatControl()
        {
            InitializeComponent();
            usernameOnline = new List<string>();

            if (!_eventRegistered)
            {
                if (_proxy != null)
                    _proxy.ProxyCallbackEvent += _proxy_ProxyCallbackEvent;

                if (_proxy != null)
                    _proxy.ProxyEvent += _proxy_ProxyEvent;
                _eventRegistered = true;
            }
            _proxy.ProxyService.Refresh();

            chatBox.Text += "Hai " + _currentClient.Username + "!\n";
        }
        
        void _proxy_ProxyCallbackEvent(object sender, ProxyCallbackEventArgs e)
        {
            Dispatcher.BeginInvoke(new Action(() =>
            {
                if (e.Sender != null)
                {
                    switch (e.CallbackType)
                    {
                        case CallbackType.ReceiveWhisper:
                            chatBox.Text += "[PM] " + e.Sender.Username + " : " + e.Message.Content + "\n";
                            break;
                        case CallbackType.ReceiveGlobalMessage:
                            chatBox.Text += e.Sender.Username + " : " + e.Message.Content + "\n";
                            break;
                        case CallbackType.ClientJoin:
                            usernameOnline.Add(e.Sender.Username);
                            chatBox.Text += e.Sender.Username + " has logged in\n";
                            break;
                        case CallbackType.ClientLeave:
                            chatBox.Text += e.Sender.Username + " has left.\n";
                            _proxy.ProxyService.Refresh();
                            break;
                    }
                }
            }));
        }

        void _proxy_ProxyEvent(object sender, ProxyEventArgs e)
        {
            if (!this.CheckAccess())
            {
                this.Dispatcher.BeginInvoke(new Action(() => this._proxy_ProxyEvent(sender, e)));
                return;
            }

            if (e.CallbackType == CallbackType.ClientRefresh)
            {
                List<Client> listUsers = e.Clients.ToList();
                userList.Items.Clear();
                foreach (Client c in listUsers)
                {
                    if(c.Username != CurrentClient.Username)
                        userList.Items.Add(c.Username);
                }
            }
        }

        public void windowClose(object sender, EventArgs e)
        {
            if (_proxy != null)
                _proxy.ProxyCallbackEvent -= _proxy_ProxyCallbackEvent;
            _eventRegistered = false;
        }

        private void sendButton_Click(object sender, RoutedEventArgs e)
        {
            string message = inputBox.Text;

           // if (broadcastCheckBox.IsChecked == true)
           // {
                _proxy.ProxyService.SendMessage(message);
           // }
           // else
           // {
             //   string target = userList.SelectedItem.ToString();
             //   _proxy.ProxyService.SendPrivateMessage(message, target);
            //}

                inputBox.Text = "";
        }

        private void clearButton_Click(object sender, RoutedEventArgs e)
        {
            chatBox.Clear();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            
          
        }
    }
}
